<?php

class ZFPainel_Grid_Deploy_Table extends Bvb_Grid_Deploy_Table
{

    //protected $_cssClasses = array('odd' => 'odd', 'even' => 'even');
    protected $_cssClasses = array('odd' => '', 'even' => '');
    private $_gridName = '';
    
    protected function _applyFieldCallback($newValue, $value, $search, $replace, $field)
    {
        return call_user_func($value['function'], $replace);
    }

    protected function _buildExtraRows($position)
    {
        $r = parent::_buildExtraRows($position);
        if (($position == 'beforePagination') and ( $r != '')) {
            $r = '<tfoot>' . $r . '</tfoot>';
        }

        return $r;
    }

    protected function _buildPagination()
    {
        return '';
    }
    
    public function deployPagination()
    {
        
        if ($this->_totalRecords == 0) {
            return '';
        }
        
        $url = $this->getUrl(array('start'));

        $actual = (int) $this->getParam('start');

        $ppagina = (int) $this->getParam('perPage');
        if ($ppagina == 0) {
            $ppagina = $this->_recordsPerPage;
        }

        // Calculate the number of pages
        if ($this->_recordsPerPage > 0) {
            $npaginas = ceil($this->_totalRecords / $ppagina);
            $actual = floor($actual / $ppagina) + 1;
        } else {
            $npaginas = 1;
            $actual = 0;
        }
        
        if ($npaginas <= 1) {
            return '';
        }
        
        $pag = '';

        $in = min(max(1, $actual - 3), $npaginas);
        $fin = min($npaginas, $actual + 3);
        
        for ($i = $in; $i <= $fin; $i++) {
            if ($i == $actual) {
                $pag .= "<li class=\"paginate_button active\"><a href=\"#\">$i</a></li>";
            } else {
                $pag .= "<li class=\"paginate_button\"><a href='$url/start{$this->getGridId()}/" . (($i - 1) * $ppagina) . "'>$i</a></li>";
            }
        }

        if ($actual != 1) {
            $pag = "<li class=\"paginate_button\"><a href=\"$url/start/0\">" . $this->__('First')
                    . "</a></li><li class=\"paginate_button\"><a href=\"$url/start{$this->getGridId()}/" . (($actual - 2) * $ppagina)
                    . "\">" . $this->__('Previous') . "</a></li>" . $pag;
        }

        if ($actual != $npaginas) {
            $pag .= "<li class=\"paginate_button\"><a href=\"$url/start{$this->getGridId()}/" . ($actual * $ppagina) . "\">"
                    . $this->__('Next') . "</a></li><li class=\"paginate_button\"><a href=\"$url/start{$this->getGridId()}/"
                    . (($npaginas - 1) * $ppagina) . "\">" . $this->__('Last') . "</a></li>";
        }

        return '<p class="pagination pull-left">Total de resultados: '. $this->_totalRecords . '</p><ul class="pagination">' . $pag . '</ul>';
    }

    public function deploy()
    {
        $content = preg_replace("#^<div id='{$this->getGridId()}'>|<\/div>$#", '', parent::deploy());

        $content = strtr($content, array(
            '{GRID_ID}' => $this->getGridId(),
        ));

        return $content;
    }
    
    public function getRequest()
    {
        if (!$this->_request) {
            $this->_request = new ZFPainel_Grid_Request();
            $this->_request->setOldRequest($this->getController()->getRequest());
        }

        return $this->_request;
    }
    
    public function getUrl($paramsToRemove = '', array $extraParams = array())
    {
        $paramsToRemove = array('start', 'strategyController', 'strategyAction', 'ajax');
        
        return parent::getUrl($paramsToRemove, $extraParams);
    }

    public function addFormatterDir($dir, $prefix)
    {
        $this->_formatter->addPrefixPath(trim($prefix, '_'), rtrim($dir, '/') . '/');
        return $this;
    }

    public function addTemplateDir($dir, $prefix, $type)
    {
        if (!isset($this->_templates[$type])) {
            $this->_templates[$type] = new Zend_Loader_PluginLoader();
        }

        $this->_templates[$type]->addPrefixPath(trim($prefix, "_"), $dir, $type);
        return $this;
    }

    public function getNameGrid()
    {
        return $this->_gridName;
    }

    public function setNameGrid($gridName)
    {
        $this->_gridName = $gridName;
    }

    protected function _buildGridTable($grids)
    {
        return '<tbody>' . parent::_buildGridTable($grids) . '</tbody>';
    }

    protected function _pagination()
    {
        return strtr(parent::_pagination(), array('&nbsp;' => ''));
    }

}
